package api

import (
	"bee/pkg/logging"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/gorilla/websocket"
	"net/http"
)

var messageChan = make(chan string)

//此处为服务端接收 需要推送的信息
func updateMsg() {
	for {
		var messageBuf string
		_, _ = fmt.Scanln(&messageBuf)
		messageChan <- messageBuf
	}
}

func EchoWS(c *gin.Context) {
	var upGrader = websocket.Upgrader{
		CheckOrigin: func(r *http.Request) bool {
			return true
		},
	}

	e, err := upGrader.Upgrade(c.Writer, c.Request, nil)
	if err != nil {
		logging.ZapLogger.Errorw("upGrade: ", err)
		return
	}

	defer e.Close()
	for {
		updateMsg()
		_ = e.WriteMessage(websocket.TextMessage, []byte(<-messageChan))
	}

}